package com.l024.android_ljl.viewmodel.login;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.entity.ObjectEntity;
import com.l024.android_ljl.entity.SysUserEntity;
import com.l024.android_ljl.repository.AppRepository;
import com.l024.android_ljl.util.RegexUtils;
import com.quantstack.llcommonlibrary.base.BaseHttpResult;
import com.quantstack.llcommonlibrary.base.BaseObserver;
import com.quantstack.llcommonlibrary.base.BaseView;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.utils.LogUtils;

import org.jetbrains.annotations.NotNull;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/5/6 16:02
 * @Notes 登录页面数据
 */
public class LoginViewModel extends BaseViewModel<SysUserEntity> {
    public MutableLiveData<SysUserEntity> sysUser;
    private AppRepository appRepository;
    public BaseView baseView;

    public void setBaseView(BaseView baseView) {
        this.baseView = baseView;
    }

    public LoginViewModel(@NonNull @NotNull Application application) {
        super(application);
        appRepository = new AppRepository();
    }

    public MutableLiveData<SysUserEntity> getSysUser() {
        if(sysUser==null){
            sysUser = new MutableLiveData<>();
            sysUser.setValue(new SysUserEntity());
        }
        return sysUser;
    }

    public void setSysUser(SysUserEntity sysUser) {
        getSysUser().setValue(sysUser);
    }

    /**
     * 登录
     */
    public void login(){
        SysUserEntity userEntity = getSysUser().getValue();
        if(userEntity!=null){
            if(RegexUtils.isPhone(userEntity.getPhone())){
                if(!TextUtils.isEmpty(userEntity.getPassword())){
                    addDisposable(appRepository.login(userEntity.getPhone(),userEntity.getPassword()),new BaseObserver<SysUserEntity>(baseView){
                        @Override
                        public void onSuccess(SysUserEntity o) {
                            if(o!=null){
                                setSysUser(o);
                            }
                            if(onPageDataListener!=null){
                                onPageDataListener.onSuccess(PageConstant.LOGIN);
                            }
                        }

                        @Override
                        public void onError(String msg) {
                            System.out.println("xxsx:"+msg);
                            if(onPageDataListener!=null){
                                onPageDataListener.onError(PageConstant.LOGIN,msg);
                            }
                        }
                    });
                }else{
                    ToastUtils.show("请输入密码");
                }
            }else{
                ToastUtils.show("请输入正确的手机号");
            }
        }
    }

    //注册
    public void register(){
        LogUtils.d("注册的数据:"+getSysUser().getValue().toString());
        SysUserEntity userEntity = getSysUser().getValue();
        if(userEntity!=null){
            String check = userEntity.check();
            if(TextUtils.isEmpty(check)){
                addDisposable(appRepository.register(userEntity),new BaseObserver<BaseHttpResult<ObjectEntity>>(baseView){
                    @Override
                    public void onSuccess(BaseHttpResult<ObjectEntity> o) {
                        if(onPageDataListener!=null){
                            onPageDataListener.onSuccess(PageConstant.REGISTER);
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        if(onPageDataListener!=null){
                            onPageDataListener.onError(PageConstant.REGISTER,msg);
                        }
                    }
                });

            }else{
                ToastUtils.show(check);
            }
        }
    }

    //上传图片
    public void uploadImage(String img){
        addDisposable(appRepository.uploadFile(img),new BaseObserver<BaseHttpResult<ObjectEntity>>(baseView){
            @Override
            public void onSuccess(BaseHttpResult<ObjectEntity> o) {
                System.out.println("图片url:"+o);
                if(o!=null){
                    setAvator(o.getData().getObject()+"");
                    ToastUtils.show("上传成功");
                }

            }

            @Override
            public void onError(String msg) {
               LogUtils.e("图片上传失败："+msg);
               ToastUtils.show(msg);
            }
        });
    }

    //设置性别
    public void setSex(int sex){
        SysUserEntity user = getSysUser().getValue();
        if(user!=null){
            if(sex==1){
                user.setSex(1);
            }else{
                user.setSex(2);
            }
            setSysUser(user);
        }

    }

    //设置头像
    public void setAvator(String url){
        SysUserEntity user = getSysUser().getValue();
        if(user!=null){
            user.setAvator(url);
            setSysUser(user);
        }
    }
}
